package com.fsnbigdata.domain;
import com.fsnbigdata.annotation.MyConstraint;
import com.fsnbigdata.util.ChineseInital;
import javax.persistence.OrderBy;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import org.hibernate.annotations.Index;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.springframework.roo.addon.javabean.annotations.RooEquals;
import org.springframework.roo.addon.javabean.annotations.RooJavaBean;
import org.springframework.roo.addon.javabean.annotations.RooToString;
import org.springframework.roo.addon.jpa.annotations.entity.RooJpaEntity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Version;
import javax.validation.Valid;
import javax.validation.constraints.*;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import org.springframework.roo.addon.jpa.annotations.entity.JpaRelationType;
import org.springframework.roo.addon.jpa.annotations.entity.RooJpaRelation;
import io.springlets.format.EntityFormat;
import java.util.Objects;
import javax.persistence.Entity;
import org.springframework.util.Assert;

/**
 * = Enterprise
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJavaBean
@RooToString
@RooJpaEntity(entityFormatExpression = "#{name}")
@RooEquals(isJpaEntity = true)
@Entity
@EntityFormat("#{name}")
public class Enterprise{

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @Version
    private Integer version;

    /**
     * 名称
     */
    @NotNull
    @Length(min=1,max=255,message="企业名称长度必须在1-255之间")
    private String name;

    /**
     * 拼音首字母
     */
    @Index(name="Enterprise_initial")
    @Length(min=0,max=255,message="拼音首字母长度必须在0-255之间")
    private String initial;

    /**
     * 类型
     */
    @Length(min=0,max=255,message="企业类型长度必须在0-255之间")
    private String category;

    /**
     * 企业法人
     */
    @NotNull(message = "企业法人不能为空")
    @Length(min=1,max=255,message="企业法人长度必须在1-255之间")
    private String legalPerson;

    /**
     * 邮编
     */
    @MyConstraint(regexp = "^[0-9]{6}$",message = "企业库邮编格式输入格式不正确")
    private String postal;

    /**
     * 电话
     */
    @MyConstraint(regexp = "^1(3|4|5|7|8)\\d{9}$",message = "企业库电话输入格式不正确")
    private String phone;

    /**
     * 传真
     */
    @MyConstraint(regexp = "^(\\d{3,4}-)?\\d{7,8}$",message = "企业库传真输入格式不正确")
    private String fax;

    /**
     * 邮箱
     */
    @MyConstraint(regexp = "^[a-z0-9]+([._\\\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$",message = "企业库邮箱输入格式不正确")
    private String email;

    /**
     * 官网
     */
    @MyConstraint(regexp = "^((https|http|ftp|rtsp|mms){0,1}(:\\/\\/){0,1})www\\.(([A-Za-z0-9-~]+)\\.)+([A-Za-z0-9-~\\/])+$",message = "企业库官网输入格式不正确")
    private String website;

    /**
     * 地址
     */
    @Length(min=0,max=255,message="企业地址长度必须在0-255之间")
    private String address;

    /**
     * 组织机构代码
     */

    @MyConstraint(regexp = "^[0-9]+$",message = "企业库组织机构代码格式不正确")
    private String organizationCode;

    /**
     * 创建者
     */
    @Length(min=0,max=255,message="企业创建者长度必须在0-255之间")
    private String creator;
    
    /**
     * 产品库：1 水果，2 蔬菜，3药用植物，4 微生物产品，5 动物产品
     */
    @Min(value = 0,message = "企业库类型最小值必须为0")
    @Max(value = 4,message = "企业库类型最大值必须为3")
    private Integer type;
    
    public Integer getType() {
        return type;
    }
    
    public void setType(Integer type) {
        this.type = type;
    }
    
    /**
     * 企业家
     */
    @OneToMany(cascade = { javax.persistence.CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "enterprise", orphanRemoval = true)
    @RooJpaRelation(type = JpaRelationType.COMPOSITION)
    @OrderBy("id")
    @Valid
    private Set<Entrepreneur> entrepreneurs = new HashSet<Entrepreneur>();

    /**
     * 生物产品
     */
    @OneToMany(cascade = { javax.persistence.CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "enterprise", orphanRemoval = true)
    @RooJpaRelation(type = JpaRelationType.COMPOSITION)
    @OrderBy("id")
    @Valid
    private Set<EnterpriseProduct> products = new HashSet<EnterpriseProduct>();

    /**
     * 生态旅游
     */
    @OneToMany(cascade = { javax.persistence.CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "enterprise", orphanRemoval = true)
    @RooJpaRelation(type = JpaRelationType.COMPOSITION)
    @OrderBy("id")
    @Valid
    private Set<EnterpriseEcosystem> ecosystems = new HashSet<EnterpriseEcosystem>();

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @OneToMany(cascade = { javax.persistence.CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "enterprise")
    @RooJpaRelation(type = JpaRelationType.COMPOSITION)
    @Valid
    private Set<EnterpriseImage> images = new HashSet<EnterpriseImage>();

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE = "The given Iterable of items to add can't be null!";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE = "The given Iterable of items to add can't be null!";

    /**
     * This `equals` implementation is specific for JPA entities and uses
     * the entity identifier for it, following the article in
     * https://vladmihalcea.com/2016/06/06/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
     *
     * @param obj
     * @return Boolean
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        // instanceof is false if the instance is null
        if (!(obj instanceof Enterprise)) {
            return false;
        }
        return getId() != null && Objects.equals(getId(), ((Enterprise) obj).getId());
    }

    /**
     * This `hashCode` implementation is specific for JPA entities and uses a fixed `int` value to be able
     * to identify the entity in collections after a new id is assigned to the entity, following the article in
     * https://vladmihalcea.com/2016/06/06/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
     *
     * @return Integer
     */
    @Override
    public int hashCode() {
        return 31;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Long
     */
    public Long getId() {
        return this.id;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param id
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Integer
     */
    public Integer getVersion() {
        return this.version;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param version
     */
    public void setVersion(Integer version) {
        this.version = version;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getName() {
        return this.name;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getInitial() {
        return this.initial;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param initial
     */
    public void setInitial(String initial) {
        this.initial = initial;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getCategory() {
        return this.category;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param category
     */
    public void setCategory(String category) {
        this.category = category;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getLegalPerson() {
        return this.legalPerson;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param legalPerson
     */
    public void setLegalPerson(String legalPerson) {
        this.legalPerson = legalPerson;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getPostal() {
        return this.postal;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param postal
     */
    public void setPostal(String postal) {
        this.postal = postal;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getPhone() {
        return this.phone;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param phone
     */
    public void setPhone(String phone) {
        this.phone = phone;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getFax() {
        return this.fax;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param fax
     */
    public void setFax(String fax) {
        this.fax = fax;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getEmail() {
        return this.email;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param email
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getWebsite() {
        return this.website;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param website
     */
    public void setWebsite(String website) {
        this.website = website;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getAddress() {
        return this.address;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param address
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getOrganizationCode() {
        return this.organizationCode;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param organizationCode
     */
    public void setOrganizationCode(String organizationCode) {
        this.organizationCode = organizationCode;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getCreator() {
        return this.creator;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param creator
     */
    public void setCreator(String creator) {
        this.creator = creator;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<Entrepreneur> getEntrepreneurs() {
        return this.entrepreneurs;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param entrepreneurs
     */
    public void setEntrepreneurs(Set<Entrepreneur> entrepreneurs) {
        this.entrepreneurs = entrepreneurs;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<EnterpriseProduct> getProducts() {
        return this.products;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param products
     */
    public void setProducts(Set<EnterpriseProduct> products) {
        this.products = products;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<EnterpriseEcosystem> getEcosystems() {
        return this.ecosystems;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param ecosystems
     */
    public void setEcosystems(Set<EnterpriseEcosystem> ecosystems) {
        this.ecosystems = ecosystems;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<EnterpriseImage> getImages() {
        return this.images;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param images
     */
    public void setImages(Set<EnterpriseImage> images) {
        this.images = images;
    }
    
    /**
     * 控制台打印
     * @return
     */
    @Override
    public String toString() {
        return "Enterprise{" +
            "id=" + id +
            ", version=" + version +
            ", name='" + name + '\'' +
            ", initial='" + initial + '\'' +
            ", category='" + category + '\'' +
            ", legalPerson='" + legalPerson + '\'' +
            ", postal='" + postal + '\'' +
            ", phone='" + phone + '\'' +
            ", fax='" + fax + '\'' +
            ", email='" + email + '\'' +
            ", website='" + website + '\'' +
            ", address='" + address + '\'' +
            ", organizationCode='" + organizationCode + '\'' +
            ", creator='" + creator + '\'' +
            ", type=" + type +
            ", entrepreneurs=" + entrepreneurs +
            ", products=" + products +
            ", ecosystems=" + ecosystems +
            ", images=" + images +
            '}';
    }
    
    /**
     * TODO Auto-generated method documentation
     *
     * @param entrepreneursToAdd
     */
    public void addToEntrepreneurs(Iterable<Entrepreneur> entrepreneursToAdd) {
        Assert.notNull(entrepreneursToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (Entrepreneur item : entrepreneursToAdd) {
            this.entrepreneurs.add(item);
            item.setEnterprise(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param entrepreneursToRemove
     */
    public void removeFromEntrepreneurs(Iterable<Entrepreneur> entrepreneursToRemove) {
        Assert.notNull(entrepreneursToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (Entrepreneur item : entrepreneursToRemove) {
            this.entrepreneurs.remove(item);
            item.setEnterprise(null);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param productsToAdd
     */
    public void addToProducts(Iterable<EnterpriseProduct> productsToAdd) {
        Assert.notNull(productsToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (EnterpriseProduct item : productsToAdd) {
            this.products.add(item);
            item.setEnterprise(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param productsToRemove
     */
    public void removeFromProducts(Iterable<EnterpriseProduct> productsToRemove) {
        Assert.notNull(productsToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (EnterpriseProduct item : productsToRemove) {
            this.products.remove(item);
            item.setEnterprise(null);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param ecosystemsToAdd
     */
    public void addToEcosystems(Iterable<EnterpriseEcosystem> ecosystemsToAdd) {
        Assert.notNull(ecosystemsToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (EnterpriseEcosystem item : ecosystemsToAdd) {
            this.ecosystems.add(item);
            item.setEnterprise(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param ecosystemsToRemove
     */
    public void removeFromEcosystems(Iterable<EnterpriseEcosystem> ecosystemsToRemove) {
        Assert.notNull(ecosystemsToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (EnterpriseEcosystem item : ecosystemsToRemove) {
            this.ecosystems.remove(item);
            item.setEnterprise(null);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param imagesToAdd
     */
    public void addToImages(Iterable<EnterpriseImage> imagesToAdd) {
        Assert.notNull(imagesToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (EnterpriseImage item : imagesToAdd) {
            this.images.add(item);
            item.setEnterprise(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param imagesToRemove
     */
    public void removeFromImages(Iterable<EnterpriseImage> imagesToRemove) {
        Assert.notNull(imagesToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (EnterpriseImage item : imagesToRemove) {
            this.images.remove(item);
            item.setEnterprise(null);
        }
    }


    @PrePersist
    public void prePersist(){
        this.setInitial(ChineseInital.getFirstLetter(this.getName()));
    }

    @PreUpdate
    public void preUpdate(){
        this.setInitial(ChineseInital.getFirstLetter(this.getName()));
    }
}
